/*
 ============================================================================
 Name		: CDirLinkList.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CCDirLinkList implementation
 ============================================================================
 */

#include "CDirLinkList.h"

//=====================================================
//CDirNode::CDirNode
//=====================================================
CDirNode::CDirNode()
{
	iParse = new(ELeave) TParse();
}
//=====================================================
//CDirNode::~CDirNode
//=====================================================
CDirNode::~CDirNode()
{
	iEntryArray.ResetAndDestroy();
	iEntryArray.Close();
	if(iParse)
	{
		delete iParse;
		iParse = NULL;
	}
}

//=====================================================
//AppendEntry
//=====================================================
void CDirNode::AppendEntry(TEntry& aEntry)
{
	iEntryArray.Append(&aEntry);
}

//=====================================================
//GetEntryArray
//=====================================================
RPointerArray<TEntry>& CDirNode::GetEntryArray()
{
	return iEntryArray;
}
//=====================================================
//CDirLinkList::CDirLinkList
//=====================================================
CDirLinkList::CDirLinkList()
{
	iHead = NULL;
	iLast = NULL;
	iCurrent = NULL;
	iNodeCount = 0;
}

//=====================================================
//CDirLinkList::~CDirLinkList
//=====================================================
CDirLinkList::~CDirLinkList()
{	
	while(iNodeCount>0)
	{
		DeleteNode();
	}
}

//=====================================================
//CDirLinkList::NewLC
//=====================================================
CDirLinkList* CDirLinkList::NewLC()
{
	CDirLinkList* self = new (ELeave) CDirLinkList();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

//=====================================================
//CDirLinkList::NewL
//=====================================================
CDirLinkList* CDirLinkList::NewL()
{
	CDirLinkList* self = CDirLinkList::NewLC();
	CleanupStack::Pop();  
	return self;
}

//=====================================================
//CDirLinkList::ConstructL
//=====================================================
void CDirLinkList::ConstructL()
{

}

//=====================================================
//AppendNode
//Append a node follow the last node
//=====================================================
void CDirLinkList::AppendNode(CDirNode& aDirNode)
{
	if(iNodeCount == 0)
	{
		iHead  = &aDirNode;
		aDirNode.iPrivious = NULL;
		aDirNode.iNext = NULL;
		iLast = iHead;	
		
	}else if(iNodeCount > 0)
	{
		iLast->iNext = &aDirNode;
		aDirNode.iPrivious = iLast;
		aDirNode.iNext = NULL;
		iLast = &aDirNode;			
	}
	iCurrent = iLast;
	iNodeCount++;
}
//=====================================================
//DeleteNode
//Delete a node from the last node
//=====================================================
void CDirLinkList::DeleteNode()
{
	if(iNodeCount == 0)
	{
		return;		
		
	}else if(iNodeCount>0)
	{
		CDirNode* pPriviousNode = iLast->iPrivious;	 		
		if(iLast)
		{
			delete iLast;
		}	
		if(pPriviousNode)
		{
			pPriviousNode->iNext = NULL;		
		}		 
		iLast = pPriviousNode;
		iCurrent = iLast;
		iNodeCount --;
	}
}

//=====================================================
//MoveNext
//Move the current to the next
//=====================================================
void CDirLinkList::MoveNext()
{
	if(iCurrent)
	{
		CDirNode* pCurentNext = iCurrent->iNext;
		if(pCurentNext)
		{
			iCurrent = pCurentNext;
		}
	}
}

//=====================================================
//MovePrivious
//Move the current to the Privious
//=====================================================
void CDirLinkList::MovePrivious()
{
	if(iCurrent)
	{
		CDirNode* pCurentPrivious = iCurrent->iPrivious;
		if(pCurentPrivious)
		{
			iCurrent = pCurentPrivious;
		}
	}
}
//=====================================================
//Current
//return the Current Node
//=====================================================
CDirNode* CDirLinkList::Current()
{
	return iCurrent;
}

//=====================================================
//Head
//Return the head node
//=====================================================
CDirNode* CDirLinkList::Head()
{
	return iHead;
}

//=====================================================
//Last
//Return the Last node
//=====================================================
CDirNode* CDirLinkList::Last()
{
	return iLast;
}
